package com.harboursoftware.xstorage.operations;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ListMultimap;
import com.harboursoftware.xstorage.XStorageException;
import com.harboursoftware.xstorage.ac.Permission;
import com.harboursoftware.xstorage.model.XElement;
import com.harboursoftware.xstorage.model.XUser;
import com.harboursoftware.xstorage.results.EmptyContentResult;
import com.harboursoftware.xstorage.results.ErrorResult;
import com.harboursoftware.xstorage.results.RedirectResult;
import com.harboursoftware.xstorage.results.Result;

public class PostObject extends Operation {
    private final static Logger log = LoggerFactory.getLogger(PostObject.class);
    @Override
    public Result execute(XUser requester, XElement element,ListMultimap<String, String> headers,
        Map<String, String> parameters) throws XStorageException {
        log.debug("excute");
        PutObject putObject = new PutObject();
        putObject.setFileSystem(fileSystem);
        Result result = putObject.execute(requester, element, headers, parameters);
        
        if(ErrorResult.class.isInstance(result)) {
            return result;
        }
        
        String redirectURL = parameters.get("success_action_redirect");
        if(redirectURL != null) {
            return new RedirectResult(redirectURL);
        }
        
        
        int status = 204; 
        String statusString = parameters.get("success_action_status");
        if(statusString != null) {
            try {
                status = Integer.valueOf(statusString);
            } catch (NumberFormatException e) {
                //TODO 异常处理
            }
            if(status != 200 || status != 201 || status != 204) {
              //TODO 异常处理
            }
        }
        return new EmptyContentResult(status);
    }

    @Override
    public Permission getRequiredPermission() {
        return Permission.WRITE;
    }

}
